#ifndef _EASY_UI_H
#define _EASY_UI_H

#include "board.h"

#include "ssd1327.h"

#define SCREEN_WIDTH            128
#define SCREEN_HEIGHT           128
#define FONT_WIDTH              8
#define FONT_HEIGHT             8
#define ITEM_HEIGHT             12
#define SCROLL_BAR_WIDTH        4
#define ITEM_LINES              10//((uint8_t)(SCREEN_HEIGHT / ITEM_HEIGHT))
#define MAX_LAYER               4

// Represent the time it takes to play the animation, smaller the quicker. Unit: ms
#define INDICATOR_MOVE_TIME     50
#define ITEM_MOVE_TIME          50

#define EasyUIScreenInit()                               (OLED_Init())
#define EasyUIDisplayStr(x, y, str)                      (u8g2_DrawStr(&u8g2, x, y+FONT_HEIGHT, str))
#define EasyUIDisplayFloat(x, y, dat, num, pointNum)     (OLED_ShowFloat(x, y+FONT_HEIGHT, dat, num, pointNum))
#define EasyUIDisplayInt16(x, y, dat)                    (OLED_ShowInt16(x, y+FONT_HEIGHT, dat))
#define EasyUIDrawDot(x, y)                              (u8g2_DrawPixel(&u8g2, x, y))
#define EasyUIDrawBox(x, y, width, height)               (u8g2_DrawBox(&u8g2, x, y, width, height))
#define EasyUIDrawRBox(x, y, width, height, r)           (u8g2_DrawRBox(&u8g2, x, y, width, height, r))
#define EasyUIDrawFrame(x, y, width, height)             (u8g2_DrawFrame(&u8g2, x, y, width, height))
#define EasyUIDrawRFrame(x, y, width, height, r)         (u8g2_DrawRFrame(&u8g2, x, y, width, height, r))
#define EasyUIClearBuffer()                              (u8g2_ClearBuffer(&u8g2))
#define EasyUISendBuffer()                               (u8g2_SendBuffer(&u8g2))
#define EasyUISetDrawColor(mode)                         (u8g2_SetDrawColor(&u8g2,mode))
#define EasyUIDisplayBMP(x, y, width, height, pic)       (u8g2_DrawXBMP(&u8g2, x, y, width, height, pic))
// #define EasyUIModifyColor()                                     (IPS114_ModifyColor())

//数值保存和读取的类型
typedef int16 paramType;

typedef enum
{
    ITEM_PAGE_DESCRIPTION,
    ITEM_JUMP_PAGE,
    ITEM_SWITCH,
    ITEM_CHANGE_VALUE,
    ITEM_PROGRESS_BAR,
    ITEM_RADIO_BUTTON,
    ITEM_CHECKBOX,
    ITEM_MESSAGE
} EasyUIItem_e;

typedef enum
{
    PAGE_LIST,
    PAGE_CUSTOM
} EasyUIPage_e;

typedef struct EasyUI_item
{
    struct EasyUI_item *next;

    uint8_t funcType;//EasyUIItem_e
    uint8_t id;
    uint8 lineId;//int16_t
    uint8 posForCal;//fp32
    uint8 step;//fp32
    uint8 position;//int16_t
    char *title;

    char *msg;                                  // ITEM_MESSAGE
    bool *flag;                                 // ITEM_CHECKBOX and ITEM_RADIO_BUTTON and ITEM_SWITCH
    // bool flagDefault;                           // Factory default setting
    paramType *param;                           // ITEM_CHANGE_VALUE and ITEM_PROGRESS_BAR
    // paramType paramDefault;                     // Factory default setting
    paramType paramBackup;                      // ITEM_CHANGE_VALUE and ITEM_PROGRESS_BAR
    uint8_t pageId;                             // ITEM_JUMP_PAGE
    void (*Event)(struct EasyUI_item *item);    // ITEM_CHANGE_VALUE and ITEM_PROGRESS_BAR
} EasyUIItem_t __attribute__((aligned(4)));

typedef struct EasyUI_page
{
    struct EasyUI_page *next;

    uint8_t funcType;//EasyUIPage_e
    EasyUIItem_t *itemHead, *itemTail;
    uint8_t id;

    void (*Event)(struct EasyUI_page *page);
} EasyUIPage_t __attribute__((aligned(4)));

typedef void (*PageFunc_t)(EasyUIPage_t *);
typedef void (*ItemFunc_t)(EasyUIItem_t *);

extern char *EasyUIVersion;
extern bool functionIsRunning, listLoop;
extern EasyUIPage_t *pageHead, *pageTail;

void EasyUIAddItem(EasyUIPage_t *page, EasyUIItem_t *item, char *_title, EasyUIItem_e func, ...);
void EasyUIAddPage(EasyUIPage_t *page, EasyUIPage_e func, ...);
void EasyUITransitionAnim();
void EasyUIBackgroundBlur();
void EasyUISyncOpnValue();

void EasyUIEventChangeUint(EasyUIItem_t *item);
void EasyUIEventChangeInt(EasyUIItem_t *item);
void EasyUIEventChangeFloat(EasyUIItem_t *item);
void EasyUIEventSaveSettings(EasyUIItem_t *item);
void EasyUIEventResetSettings(EasyUIItem_t *item);

void EasyUIInit(uint8_t mode);
void EasyUI(uint8_t timer);

void OLED_ShowFloat(uint8 x, uint8 y, paramType dat, uint8 num, uint8 pointNum);
void OLED_ShowInt16(uint8 x, uint8 y, int16 dat);

#endif
